package com.xxx.test;

import com.xxx.DAO.commentDao;
import com.xxx.entity.Book;
import com.xxx.entity.Comment;
import com.xxx.util.JDBCUtil;
import com.xxx.util.pageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/showComment")
public class showComment extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int iPageSize = 4, iTotal = 0, iPageCnt = 0, iPage = 0;
        if (request.getParameter("page") != null) {// 从翻页操作链接进入
            try {
                iPage = Integer.parseInt(request.getParameter("page"));
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        if (iPage < 1) { iPage = 1; }
        request.setAttribute("iPage",iPage);
        commentDao dao=new commentDao();
        List<Comment> commentList = new ArrayList<>();//总的数据集合
        List<Comment> commentList2 = new ArrayList<>();//分页后的数据集合
        pageUtil pageutil=new pageUtil();//创建分页类
        JDBCUtil util=new JDBCUtil();
        commentList=dao.findAll();
        iTotal = pageutil.getPageCount(commentList);
        iPageCnt = (iTotal + iPageSize - 1) / iPageSize;
        if (iPage > iPageCnt) {
            iPage = iPageCnt;
        }
        if (iPageCnt > 0) {
            commentList2 = pageUtil.startPage(commentList, iPage, 4);//分页后的book类list
            request.setAttribute("list", commentList2);
            request.setAttribute("iPageCnt", iPageCnt);
        }
        request.getRequestDispatcher("comment.jsp").forward(request,response);
    }
}
